import re

from django.core.exceptions   import ValidationError
from django.utils.translation import ugettext_lazy as _

class PathValidator(object):
    RE_PART  = re.compile(r'^((?:(?P<var>%)|(?P<part>\w+))/?)$')
    RE_SPLIT = re.compile(r'/')
    
    def __call__(self,value):
        if value.endswith('/'):
            raise ValidationError(_(u"Path can not end with a slash"),code='invalid')
        for part in self.RE_SPLIT.split(value):
            if not self.RE_PART.match(part):
                raise ValidationError(_(u"Path parts must consist of underscores, letters or digits or a '%'-sign as placeholder "),code='invalid')

validate_path = PathValidator()

def validate_view_args(value):
    if not isinstance(value,(tuple,list)):
        raise ValidationError,"must be a list"
    for i in xrange(len(value)):
        if not isinstance(value[i],basestring):
            raise ValidationError,"argument {0} must be a string".format(i)

